#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <pthread.h>
#include <pthread.h>
#include <sys/types.h>
#include <memory>
#include "InetAddr.hpp"
#include "log.hpp"
// 模版方法模式---套接字的封装
namespace socket_ns
{
    class mysocket;
    using socket_sptr = std::shared_ptr<mysocket>; // 指向mysocket类的指针
    static const int gbacklog = 8;
    enum
    {
        SOCKET_ERROR = 1,
        BIND_ERROR,
        LISTEN_ERROR,
        USAGE_ERROR
    };

    class mysocket
    {
    public:
        virtual void create_socket_or_die() = 0;
        virtual void bing_socket_or_die(InetAddr &addr) = 0;
        virtual void listen_socket_or_die() = 0;
        virtual int accepter(InetAddr *addr) = 0;
        virtual bool connectcor(InetAddr &addr) = 0;
        virtual void set_sock_addr_reuse() = 0;//服务器设置地址复用
        virtual int SockFd() = 0;
        virtual int socked_recv(std::string *out) = 0;
        virtual int socked_send(const std::string &in) = 0;
        virtual void close_sockfd() = 0;

    public:
        void build_server_socket(InetAddr &addr) // 处理服务端
        {
            create_socket_or_die();
            set_sock_addr_reuse();
            bing_socket_or_die(addr);
            listen_socket_or_die();
        }
        bool build_client_socket(InetAddr &addr) // 处理客户端
        {
            create_socket_or_die();
            return connectcor(addr);
        }
    };

    class tcp_socket : public mysocket
    {
    public:
        tcp_socket(int fd = -1)
            : _sockfd(fd)
        {
        }
        void create_socket_or_die() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(FATAL, "socket error\n");
                exit(SOCKET_ERROR);
            }
            LOG(DEBUG, "socket create seccess ,sockfd is : %d\n", _sockfd);
        }
        void bing_socket_or_die(InetAddr &addr) override
        {
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(addr.Port());
            local.sin_addr.s_addr = inet_addr(addr.Ip().c_str());
            int n = ::bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
            if (n < 0)
            {
                LOG(FATAL, "bind error\n");
                exit(BIND_ERROR);
            }
            LOG(DEBUG, "bind success, sockfd is :%d\n", _sockfd);
        }
        void listen_socket_or_die() override
        {
            int n = ::listen(_sockfd, gbacklog); // 将一个未连接的套接字转换为一个被动套接字，即监听套接字，它可以接受其他套接字的连接请求
            if (n < 0)
            {
                LOG(FATAL, "listen error\n");
                exit(LISTEN_ERROR);
            }
            LOG(DEBUG, "listen success, sockfd is : %d\n", _sockfd);
        }
        int accepter(InetAddr *addr) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sockfd = ::accept(_sockfd, (struct sockaddr *)&peer, &len);
            if (sockfd < 0)
            {
                LOG(WARNING, "accept error\n");
                return -1;
            }
            *addr = peer;
            // socket_sptr sock = std::make_shared<tcp_socket>(sockfd);
            return sockfd;
        }
        virtual bool connectcor(InetAddr &addr)
        {
            // 设置本地信息
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_port = htons(addr.Port());
            server.sin_addr.s_addr = inet_addr(addr.Ip().c_str());
            // 连接套接字sockfd
            int n = connect(_sockfd, (struct sockaddr *)&server, sizeof(server));
            if (n < 0)
            {
                std::cerr << "connect error" << std::endl;
                return false;
            }
            return true;
        }
        int socked_send(const std::string &in) override
        {
            int n = ::send(_sockfd, in.c_str(), in.size(), 0);
            return n;
        }
        int socked_recv(std::string *out) override
        {
            char buffer[1024];
            ssize_t n = recv(_sockfd, buffer, sizeof(buffer) - 1, 0); // 接收来自套接字sockfd中的信息
            if (n > 0)
            {
                buffer[n] = 0;
                *out += buffer;
            }
            return n;
        }
        void set_sock_addr_reuse() override
        {
            int opt = 1;
            ::setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
        }
        virtual void close_sockfd() override
        {
            ::close(_sockfd);
        }
        int SockFd() override
        {
            return _sockfd;
        }

    private:
        int _sockfd;
    };

}
